
package com.gp.activity.main;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

import com.gp.R;
import com.gp.activity.base.BaseActivity;
import com.gp.activity.detail.MywayDetailActivity;
import com.gp.adapter.GrideViewAdapeter;
import com.gp.model.MywayGrideItem;
import com.gp.utils.DatabaseManager;

public class MyWayActivity extends BaseActivity {

    //查看
    public static final int CHECK_STATE = 0;
    //编辑
    public static final int EDIT_STATE = 1;
    //修改
    public static final int ALERT_STATE = 2;

    private Button mAddButton;
    private Button mBackButton;
    private GrideViewAdapeter mGrideViewAdapeter;
    private GridView mGridView;

    // 数据库管理对象
    private DatabaseManager mDatabaseManager;
    // 游标
    private Cursor mCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myway);
        initView();
        bindData();
        initListener();
    }

    @Override
    protected void onRestart() {
        bindData();
        super.onRestart();
    }
   
    private void initView() {

        mAddButton = (Button) findViewById(R.id.btn_myway_addway);
        mGridView = (GridView) findViewById(R.id.gv_maway_gridview);
        mBackButton = (Button) findViewById(R.id.btn_myway_back);

    }

    private void initListener() {

        // GridView选项长按的监听

        mGridView
                .setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

                    @Override
                    public void onCreateContextMenu(ContextMenu menu, View v,
                            ContextMenuInfo menuInfo) {
                        final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                        menu.setHeaderTitle("");
                        menu.add(0, 0, 0, "删除");
                        menu.add(0, 1, 0, "修改");
                    }

                });

        // 添加按钮的监听
        mAddButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("state", EDIT_STATE);
                intent.setClass(MyWayActivity.this, MywayDetailActivity.class);
                MyWayActivity.this.startActivity(intent);
            }
        });

        //添加返回按钮
        mBackButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                MyWayActivity.this.finish();
            }
        });
        // GridView选项点击的监听
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                try {
                    mCursor.moveToPosition(position);
                    Intent intent = new Intent();

                    intent.putExtra("state", CHECK_STATE);
                    intent.putExtra("id",
                            mCursor.getString(mCursor.getColumnIndex("_id")));
                    intent.putExtra("title",
                            mCursor.getString(mCursor.getColumnIndex("title")));
                    intent.putExtra("time",
                            mCursor.getString(mCursor.getColumnIndex("time")));
                    intent.putExtra("content", mCursor.getString(mCursor
                            .getColumnIndex("content")));

                    intent.setClass(MyWayActivity.this,
                            MywayDetailActivity.class);
                    MyWayActivity.this.startActivity(intent);
                    MyWayActivity.this.finish();

                }
                catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void bindData() {

        mDatabaseManager = new DatabaseManager(this);
        // 打开数据库管理对象
        mDatabaseManager.open();

        // 得到数据集合
        mCursor = mDatabaseManager.selectNoteAll();
        // 游标置顶
        mCursor.moveToFirst();

        int qCount = mCursor.getCount();

        List<MywayGrideItem> qGrideItems = new ArrayList<MywayGrideItem>();

        for (int i = 0; i < qCount; i++) {

            MywayGrideItem qGrideItem = new MywayGrideItem(
                    mCursor.getString(mCursor.getColumnIndex("title")),
                    mCursor.getString(mCursor.getColumnIndex("content")),
                    mCursor.getString(mCursor.getColumnIndex("time")));

            qGrideItems.add(qGrideItem);
            mCursor.moveToNext();
        }
        mDatabaseManager.close();
        // 设置适配器
        mGrideViewAdapeter = new GrideViewAdapeter(this, qGrideItems);
        mGridView.setAdapter(mGrideViewAdapeter);
    }

    // 长按后的事件
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        mDatabaseManager.open();
        switch (item.getItemId()) {

            case 0:
                try {
                    mCursor.moveToPosition(menuInfo.position);

                    int i = mDatabaseManager.delete(Long.parseLong(mCursor
                            .getString(mCursor.getColumnIndex("_id"))));

                    mGrideViewAdapeter.removeItem(menuInfo.position);
                    mGrideViewAdapeter.notifyDataSetChanged();// 通知数据源，数据已经改变，刷新界面

                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            // 修改
            case 1:
                try {
                    mCursor.moveToPosition(menuInfo.position);
                    Intent intent = new Intent();

                    intent.putExtra("state", ALERT_STATE);
                    intent.putExtra("id",
                            mCursor.getString(mCursor.getColumnIndex("_id")));
                    intent.putExtra("title",
                            mCursor.getString(mCursor.getColumnIndex("title")));
                    intent.putExtra("time",
                            mCursor.getString(mCursor.getColumnIndex("time")));
                    intent.putExtra("content", mCursor.getString(mCursor
                            .getColumnIndex("content")));

                    intent.setClass(MyWayActivity.this,
                            MywayDetailActivity.class);
                    MyWayActivity.this.startActivity(intent);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            default:
                ;
        }
        mDatabaseManager.close();
        return super.onContextItemSelected(item);
    }
}
